import { _decorator, Label, ProgressBar, Node } from 'cc';
import { BaseWindow } from '../../core/runtime/UIFramework';

const { ccclass, property } = _decorator;

@ccclass('LoadingView')
export class LoadingView extends BaseWindow {
    @property(Label)
    private titleLabel: Label | null = null;

    @property(Label)
    private progressLabel: Label | null = null;

    @property(ProgressBar)
    private progressBar: ProgressBar | null = null;

    @property(Node)
    private background: Node | null = null;

    private _isLoading: boolean = false;
    private _currentProgress: number = 0;
    private _targetProgress: number = 0;

    protected onLoad(): void {
        this.init();
    }

    protected update(deltaTime: number): void {
        // 平滑更新进度条
        if (this._currentProgress < this._targetProgress) {
            this._currentProgress += deltaTime * 2; // 调整这个值可以改变动画速度
            if (this._currentProgress > this._targetProgress) {
                this._currentProgress = this._targetProgress;
            }
            this.updateProgressDisplay();
        }
    }

    init(): void {
        this._isLoading = false;
        this._currentProgress = 0;
        this._targetProgress = 0;
        this._uiName = "LoadingView";
        
        // 初始化UI显示
        this.updateProgressDisplay();
        
        if (this.titleLabel) {
            this.titleLabel.string = "加载中...";
        }
    }

    /**
     * 窗口打开时调用
     * @param data 可选数据
     */
    onOpen(data?: any): void {
        super.onOpen(data);
        this.init();
        this.reset();
    }

    /**
     * 窗口关闭时调用
     */
    onClose(): void {
        super.onClose();
        this.reset();
        this._isLoading = false;
    }

    /**
     * 设置进度值
     * @param progress 进度值 (0-1)
     * @param title 可选标题文本
     */
    public setProgress(progress: number, title?: string): void {
        // 限制进度值在0-1之间
        this._targetProgress = Math.max(0, Math.min(1, progress));
        
        // 更新标题（如果提供）
        if (title && this.titleLabel) {
            this.titleLabel.string = title;
        }
    }

    /**
     * 更新进度显示
     */
    private updateProgressDisplay(): void {
        // 更新进度条
        if (this.progressBar) {
            this.progressBar.progress = this._currentProgress;
        }
        
        // 更新进度文本
        if (this.progressLabel) {
            this.progressLabel.string = `${Math.floor(this._currentProgress * 100)}%`;
        }
    }

    /**
     * 设置标题
     * @param title 标题文本
     */
    public setTitle(title: string): void {
        if (this.titleLabel) {
            this.titleLabel.string = title;
        }
    }

    /**
     * 重置加载界面
     */
    public reset(): void {
        this._currentProgress = 0;
        this._targetProgress = 0;
        this.updateProgressDisplay();
        
        if (this.titleLabel) {
            this.titleLabel.string = "加载中...";
        }
    }

    /**
     * 显示错误信息
     * @param message 错误信息
     */
    public showError(message: string): void {
        this._isLoading = false;
        if (this.titleLabel) {
            this.titleLabel.string = message;
        }
        
        if (this.progressLabel) {
            this.progressLabel.string = "错误";
        }
        
        if (this.progressBar) {
            this.progressBar.progress = 0;
        }
    }

    /**
     * 显示完成状态
     * @param message 完成信息
     */
    public showComplete(message: string = "加载完成"): void {
        this._targetProgress = 1;
        this._currentProgress = 1;
        this.updateProgressDisplay();
        
        if (this.titleLabel) {
            this.titleLabel.string = message;
        }
        
        // 自动隐藏界面
        this.scheduleOnce(() => {
            this.close();
        }, 0.5);
    }
}